package com.zzccyy.jpdemo.page

import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.google.gson.Gson
import com.zzccyy.jpdemo.R
import com.zzccyy.jpdemo.base.BasicBaseActivity
import com.zzccyy.jpdemo.bean.CityLoBean
import com.zzccyy.jpdemo.bean.JpCityBean
import com.zzccyy.jpdemo.utils.HttpUtils
import com.zzccyy.jpdemo.utils.HttpUtils.HttpCallback
import com.zzccyy.jpdemo.utils.JpCityAdapter
import com.zzccyy.jpdemo.utils.JpCityAdapter1


class JpCityActivity : BasicBaseActivity() {
    private var ivC: View? = null
    private var llrr: View? = null
    private var nestSc: View? = null
    private var llooc: View? = null
    private var et: EditText? = null
    private var tvCCC: TextView? = null
    private var recyClerView: RecyclerView? = null
    private var recyClerView1: RecyclerView? = null
    private var recyclerView2: RecyclerView? = null
    private var jpCityList: List<JpCityBean.DataBean>? = null
    private var jpCityAdapter2: JpCityAdapter1? = null
    private var jpCityAdapter: JpCityAdapter? = null
    private var jpCityAdapter1: JpCityAdapter? = null
    private var from = 0


    override val layoutId: Int
        get() = R.layout.activity_city_jp

    override fun findViews() {
        ivC = findViewById(R.id.ivC)
        et = findViewById(R.id.et)
        recyClerView = findViewById(R.id.recyClerView)
        recyClerView1 = findViewById(R.id.recyClerView1)
        recyclerView2 = findViewById(R.id.recyclerView2)
        nestSc = findViewById(R.id.nestSc)
        tvCCC = findViewById(R.id.tvCCC)
        llrr = findViewById(R.id.llrr)
        llooc = findViewById(R.id.llooc)
        val gridLayoutManager = GridLayoutManager(this, 4)
        gridLayoutManager.orientation = LinearLayoutManager.VERTICAL
        recyClerView!!.setLayoutManager(gridLayoutManager)
        val gridLayoutManager1 = GridLayoutManager(this, 4)
        gridLayoutManager1.orientation = LinearLayoutManager.VERTICAL
        recyClerView1!!.setLayoutManager(gridLayoutManager1)

        recyclerView2!!.setLayoutManager(LinearLayoutManager(this))
        val strings: MutableList<String> = ArrayList()
        strings.add("北京")
        strings.add("上海")
        strings.add("广州")
        strings.add("深圳")
        strings.add("天津")
        strings.add("重庆")
        strings.add("西安")
        strings.add("成都")
        strings.add("杭州")
        strings.add("南京")
        strings.add("苏州")
        strings.add("武汉")
        jpCityAdapter = JpCityAdapter(R.layout.item_jp_city, strings)
        recyClerView!!.setAdapter(jpCityAdapter)


        val strings1: MutableList<String> = ArrayList()
        strings1.add("A")
        strings1.add("B")
        strings1.add("C")
        strings1.add("D")
        strings1.add("E")
        strings1.add("F")
        strings1.add("G")
        strings1.add("H")
        strings1.add("I")
        strings1.add("J")
        strings1.add("K")
        strings1.add("L")
        strings1.add("M")
        strings1.add("N")
        strings1.add("O")
        strings1.add("P")
        strings1.add("Q")
        strings1.add("R")
        strings1.add("S")
        strings1.add("T")
        strings1.add("U")
        strings1.add("V")
        strings1.add("W")
        strings1.add("X")
        strings1.add("Y")
        strings1.add("Z")
        jpCityAdapter1 = JpCityAdapter(R.layout.item_jp_city, strings1)
        recyClerView1!!.setAdapter(jpCityAdapter1)

        jpCityAdapter2 = JpCityAdapter1(R.layout.item_jp_city2)
        recyclerView2!!.setAdapter(jpCityAdapter2)
    }

    override fun setListener() {
        ivC!!.setOnClickListener { v: View? ->
            finish()
        }
        llrr!!.setOnClickListener { v: View? ->
            lo
        }
        llooc!!.setOnClickListener { v: View? ->
            if (tvCCC!!.text.toString() != "定位") {
                extracted(tvCCC!!.text.toString())
            }
        }
        jpCityAdapter!!.onItemClickListener =
            BaseQuickAdapter.OnItemClickListener { adapter, view, position ->
                val s = jpCityAdapter!!.data[position]
                extracted(s)
            }
        jpCityAdapter1!!.onItemClickListener =
            BaseQuickAdapter.OnItemClickListener { adapter, view, position ->
                val s = jpCityAdapter1!!.data[position]
                search(s)
            }
        jpCityAdapter2!!.onItemClickListener =
            BaseQuickAdapter.OnItemClickListener { adapter, view, position ->
                val s = jpCityAdapter2!!.data[position]
                extracted(s)
            }
    }

    private fun extracted(s: String) {
        val intent1 = intent
        intent1.putExtra("city", s)
        setResult(1000, intent1)
        finish()
    }

    private fun search(s: String) {
        if (!TextUtils.isEmpty(s)) {
            nestSc!!.visibility = View.GONE
            recyclerView2!!.visibility = View.VISIBLE
            val strings = ArrayList<String?>()
            if (jpCityList != null) {
                for (i in jpCityList!!.indices) {
                    val dataBean = jpCityList!![i]
                    if (dataBean.cityName!!.contains(s)) {
                        strings.add(dataBean.cityName)
                    }
                    if (dataBean.groupName!!.contains(s)) {
                        strings.add(dataBean.cityName)
                    }
                }
                jpCityAdapter2!!.setNewData(strings)
            }
        } else {
            nestSc!!.visibility = View.VISIBLE
            recyclerView2!!.visibility = View.GONE
        }
    }

    override fun initData() {
        val intent = intent
        from = intent.getIntExtra("from", 1)
        lo
        getJpCityList()

        et!!.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
                // 输入前的回调
            }

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable) {
                // 文本变化后的回调
                search(s.toString())
            }
        })
    }

    private val lo: Unit
        get() {
            val url = "https://api.tgelia.com/api/train/getPosCity"
            val params: Map<String, String> = HashMap()
            HttpUtils.getInstance().postJson(url, params, object : HttpCallback {
                override fun onSuccess(result: String) {
                    val cityLoBean = Gson().fromJson(result, CityLoBean::class.java)
                    if (cityLoBean != null && cityLoBean.code == 200) {
                        val cityName = cityLoBean.data!!.cityName
                        tvCCC!!.text = cityName
                    } else {
                        Toast.makeText(this@JpCityActivity, "当前日期没数据", Toast.LENGTH_SHORT)
                            .show()
                    }
                }

                override fun onFailure(errorMsg: String) {
                    Toast.makeText(this@JpCityActivity, "查询失败", Toast.LENGTH_SHORT).show()
                }
            })
        }

    private fun getJpCityList() {
        //showLoading()
        var url = ""
        url = if (from == 1) {
            "https://api.tgelia.com/api/train/getAirplaneCityList"
        } else {
            "https://api.tgelia.com/api/train/getCityList"
        }
        val params: Map<String, String> = HashMap()
        HttpUtils.getInstance().postJson(url, params, object : HttpCallback {
            override fun onSuccess(result: String) {
                dissmissLoading()
                val cityLoBean = Gson().fromJson(result, JpCityBean::class.java)
                if (cityLoBean != null && cityLoBean.code == 200) {
                    jpCityList = cityLoBean.data
                }
            }

            override fun onFailure(errorMsg: String) {
                dissmissLoading()
            }
        })
    }
}